package com.miyake.loveviewweb.client.chart;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.googlecode.gchart.client.GChart;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.shared.model.ChartModel;

public class SimpleDynamicChart extends GChart implements IChart{

    private ChartModel chartModel;

    public void setChartTitle(String html) {
        super.setChartTitle("<b>" + html + "</b>");
    }

    public SimpleDynamicChart() {
        
            setChartSize(300, 200);
            addCurve();
       // solid, 2px thick, 1px resolution, connecting lines:
            getCurve().getSymbol().setSymbolType(SymbolType.LINE);
            //getCurve().getSymbol().setFillThickness(0);
            //getCurve().getSymbol().setFillSpacing(0);
            getCurve().getSymbol().setWidth(1);
            getCurve().getSymbol().setHeight(1);
       // Make center-fill of square point markers same color as line:
        getCurve().getSymbol().setBackgroundColor(
                getCurve().getSymbol().getBorderColor());
//        for (int i = 0; i < 10; i++) 
//          getCurve().addPoint(i,i*i);
        
        getXAxis().setAxisLabel("x");
        getYAxis().setAxisLabel("Y");
    }

    public void setData(String xname, double[] x, String yname, double[] y) {
        for (int i = 0; i < y.length; i++) {
            getCurve().addPoint(x[i], y[i]); 
        } 
    }

    public void setData(String ylegend, double[] y) {
        for (int i = 0; i < y.length; i++) {
            getCurve().addPoint(i, y[i]);
        }  
        getCurve().setLegendLabel(ylegend);
    }

    public Composite getUiWidget() {
        return this;
    }

    public void clear() {
        this.clearCurves();
    }

    public void clearPlot(int i) {
        this.getCurve(i).clearPoints();
    }

    public void setChartSize(Integer width, Integer height) {
        super.setChartSize(width, height);
        new ChartSizeUpdater(chartModel.getKey(), width, height);
    }

    public void setData(final ChartModel chartModel) {
        this.chartModel = chartModel;
        DataServiceWrapper.getInstance().getService().getChart(chartModel.getKey(), true, new AsyncCallback<ChartModel>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(ChartModel result) {
                if (result.getChartDataType().equals(ChartModel.ChartDataType.XY_DATA)) {
                    setData(
                        result.getxLabel(), result.getXarray(), 
                        result.getyLabel(), result.getYarray());
                }
                else {
                    setData(result.getyLabel(), result.getYarray());
                }
                getXAxis().setAxisLabel(result.getxLabel());
                getYAxis().setAxisLabel(result.getyLabel()); 
                setChartTitle("<b>"+ result.getName() + "</b>");
                setChartSize(chartModel.getChartWidth(), chartModel.getChartHeight());
                update();
            }
            
        });

    }
    
    
}
